#! /usr/bin/bash
. $LKP_SRC/lib/log.sh

qcow2_domain=$qcow2_os-$qcow2_version-$qcow2_arch
iso_filename=$(basename $iso_url)
iso_path="$RESULT_ROOT"/$iso_filename
qcow2_path="$RESULT_ROOT/qcow2/$qcow2_domain.qcow2"

iso_checksum()
{
	local iso_path=$1
	local sha256sum_standard=$2

	sha256sum_iso=$(sha256sum "${iso_path}" | awk '{print $1}')
	if [ "${sha256sum_iso}" == "${sha256sum_standard}" ]; then
		return 0
	else
		return 1
	fi
}

wget_file_with_delete()
{
	local local_path=$1
	local net_url=$2

	[ -d "$(dirname "$local_path")" ] ||
		mkdir -p "$(dirname "$local_path")"
	[ -f "$local_path" ] &&
		mv "$local_path" "$local_path"."$(date "+%Y%m%d-%H%M%S")".bak
	wget --no-check-certificate -c -O "$local_path" "$net_url"
}

prepare_uefi()
{
	log_info "starting prepare edk2 UEFI..."
	if [ "$qcow2_arch" == "aarch64" ]; then
		dnf install -y edk2-aarch64

		if [ ! -d "/usr/share/edk2/aarch64/QEMU_EFI-pflash.raw"] && [! -d "/usr/share/edk2/aarch64/QEMU_EFI.fd" ]; then
			log_error "lack of QEMU_EFI-pflash.raw and QEMU_EFI.fd, cannot start to install."
			exit 1
		fi
	elif [ "$qcow2_arch" == "x86_64" ]; then
		dnf install -y edk2-ovmf

		if [ ! -d "/usr/share/edk2/ovmf/OVMF_CODE.fd"] && [! -d "/usr/share/edk2/ovmf/OVMF.fd" ]; then
			log_error "lack of OVMF_CODE.fd and OVMF.fd, cannot start to install."
			exit 1
		fi
	fi
	log_info "edk2 UEFI has been prepared"
}

prepare_libvirtd()
{
	log_info "check whether libvirtd.service exists"
	if systemctl is-active --quiet libvirtd; then
		log_info "libvirtd service is already running"
	elif systemctl list-unit-files --quiet libvirtd.service; then
		systemctl start libvirtd
		log_info "libvirtd.service has been started"
	else
		log_error "libvird.service could not been normally started"
		exit 1
	fi
}

create_qcow2()
{
	log_info "starting create_qcow2..."
	[ -d "$(dirname "${qcow2_path}")" ] ||
		mkdir -m 775 -p "$(dirname "${qcow2_path}")"
		chown qemu:qemu $(dirname $qcow2_path)
	[ -f "${qcow2_path}" ] &&
		rm -f "${qcow2_path}"
	qemu-img create -f qcow2 $qcow2_path "$capacity"
}

install_os()
{
	log_info "starting libvirt_install..."
	virsh list | grep -wq "${qcow2_domain}" && virsh destroy "${qcow2_domain}" --nvram

	local qcow2_xml=$RESULT_ROOT/$qcow2_domain.xml
	cp $LKP_SRC/programs/iso2qcow2/template.xml $RESULT_ROOT/$qcow2_domain.xml
	chmod 775 $iso_path
	chmod 775 $qcow2_path
    
	sed -i "s|QCOW2_ARCH|${qcow2_arch}|g" "${qcow2_xml}"
	sed -i "s|ISO_PATH|${iso_path}|g" "${qcow2_xml}"
	sed -i "s|QCOW2_PATH|${qcow2_path}|g" "${qcow2_xml}"
	sed -i "s|QCOW2_DOMAIN|${qcow2_domain}|g" "${qcow2_xml}"
	sed -i "s|MEMORY|${memory}|g" "${qcow2_xml}"
	sed -i "s|VCPUS|${vcpus}|g" "${qcow2_xml}"
	sed -i "s|CPU_MODE|${cpu_mode}|g" "${qcow2_xml}"
	sed -i "s|SOCKETS|${sockets}|g" "${qcow2_xml}"
	sed -i "s|CORES|${cores}|g" "${qcow2_xml}"
	sed -i "s|THREADS|${threads}|g" "${qcow2_xml}"

	[ "$qcow2_arch" == "aarch64" ] && {
		sed -i "s|PFLASH_PATH|/usr/share/edk2/aarch64/QEMU_EFI-pflash.raw|g" "${qcow2_xml}"
	}

	[ "$qcow2_arch" == "x86_64" ] &&  {
		sed -i "s|PFLASH_PATH|/usr/share/edk2/ovmf/OVMF_CODE.fd|g" "${qcow2_xml}"
	}

	$LKP_SRC/lib/ks_install.exp \
		$LKP_SRC/distro/ks/${qcow2_os,,}/$qcow2_domain.ks \
		$RESULT_ROOT/$qcow2_domain.xml

	log_info "create qcow2 over, path: $(pwd)/$qcow2_domain.qcow2"
}

download_iso()
{
	log_info "Start to download $(basename $iso_url)..."
	wget_file_with_delete $iso_path $iso_url

	# download iso signature
	if [ -n $iso_checksum_url ];then
		iso_checksum_filename=$(basename $iso_checksum_url)

		log_info "Start to download $iso_checksum_filename..."
		wget_file_with_delete $RESULT_ROOT/$iso_checksum_filename $iso_checksum_url
		
		iso_checksum $iso_path $(cat $RESULT_ROOT/$iso_checksum_filename)
	fi
}

prepare_libvirtd
prepare_uefi
download_iso
create_qcow2
install_os